package com.lz.sports.bean

import android.os.Parcel
import android.os.Parcelable
import com.lz.library.proguard.IProguardField
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
import com.lz.sports.LogUtils
import java.util.*

//考试项目
class ExamProject() : ParcelableString, IProguardField {

    //唯一值 其它关联表的外键
    @Expose
    var id: Int = 0

    @Expose
    var projectId: Int = 0

    //计划编号
    @Expose
    var examPlanId: Int = 0

    @Expose
    var areaId: Int = 0

    //项目名称
    @Expose
    var projectName: String = ""

    //考试开始日期  2023-10-28 08:00:00, 前面日期代表考试时间天, 后面的时分点代表考试当天的时间限制
    @Expose
    var projectStartTime: Long = 0L

    //考试结束日期  2023-10-28 18:00:00
    @Expose
    var projectEndTime: Long = 0L

    //评分规则
    @SerializedName("busProjectConfigDTOList")
    @Expose
    var projectConfig: ProjectConfig? = null

    //当前项目是否在有效时间内
    fun isEffective(): Boolean {
        if (projectStartTime <= 0L || projectEndTime <= 0L) return false
        val curTime = System.currentTimeMillis()
        LogUtils.i("$projectStartTime - $projectEndTime - $curTime")

        if (curTime < projectStartTime || curTime > projectEndTime) return false
        val startHour = dateHourTime(projectStartTime)
        val startDay = projectStartTime - startHour
        val endHour = dateHourTime(projectEndTime)
        val endDay = projectEndTime - endHour

        val curHour = dateHourTime(curTime)
        val curDay = curTime - curHour
        LogUtils.i("$startDay - $endDay, $startHour - $endHour, $curDay - $curHour")
        return curDay in startDay .. endDay && curHour in startHour .. endHour
    }

    //获取当天的小时分秒时间值
    private fun dateHourTime(time: Long): Long {
        val calendar = Calendar.getInstance().apply { timeInMillis = time }
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        val minute = calendar.get(Calendar.MINUTE)
        val second = calendar.get(Calendar.SECOND)
        return hour * 3600L * 1000L + minute * 60 * 1000L + second * 1000L
    }

    constructor(parcel: Parcel) : this() {
        id = parcel.readInt()
        projectId = parcel.readInt()
        examPlanId = parcel.readInt()
        areaId = parcel.readInt()
        projectName = parcel.readString()!!
        projectStartTime = parcel.readLong()
        projectEndTime = parcel.readLong()
        projectConfig = parcel.readParcelable(ProjectConfig::class.java.classLoader)
    }

    override fun str(): String = projectName

    override fun equals(other: Any?): Boolean = other === this || (other is ExamProject && other.id == id)

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(id)
        parcel.writeInt(projectId)
        parcel.writeInt(examPlanId)
        parcel.writeInt(areaId)
        parcel.writeString(projectName)
        parcel.writeLong(projectStartTime)
        parcel.writeLong(projectEndTime)
        parcel.writeParcelable(projectConfig, flags)


    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<ExamProject> {
        override fun createFromParcel(parcel: Parcel): ExamProject {
            return ExamProject(parcel)
        }

        override fun newArray(size: Int): Array<ExamProject?> {
            return arrayOfNulls(size)
        }
    }
}